﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php

//patikrinti ar nera id is GET
if (isset($_GET['userid'])) {
$edit = true;
};

//patikrint ar nera tokio bylos nr, 
// Jeigu forma nauja ir submitinta - tikrina, jeigu ne - ne (logichno?)
	if (($edit == FALSE) && isset($_POST['Submit']) && ($_POST['Submit'])!=null ){
		$sql = "SELECT * ";
		$sql .= "FROM `user` ";
		$sql .= "WHERE `user`.bylosnr = ";
		$sql .= "'". ($_POST['bylosnr']). "'";
		//procesinam query	
				$result = mysql_query($sql, $connection);
				$tmp = mysql_fetch_array($result);
			if (($tmp['bylosnr'])!= NULL) {
				// Success!
				$message .= "</br>Toks bylos Nr jau yra </br>";
		//----------------------------------------------------------------------//
		//         DADET Table su useriu kuris turi toki bylos NR.				//
		//----------------------------------------------------------------------//
			} else {
				// Display error message.
				$message .= "<p>Naujas Bylos Nr</p>";
				//echo "<p>" . mysql_error() . "</p>";
			};
	} ;


// Jeigu yra GET bet nera POST pildom forma is duombazes
	if ($edit && !isset($_POST['Submit'])) {
		$message = "</br> Gavau userid is praeito puslapio, reikes redaguot </br>";
	//--------------------------------------------------------------------------//
	//  query pagal userid, zpildyk kintamasias formoi, pakeis knopkes uzrasus	//
	//--------------------------------------------------------------------------//	
		
		$sql = "SELECT * ";
		$sql .= "FROM `user` ";
		$sql .= "WHERE `user`.userid = ";
		$sql .= "'". ($_GET['userid']). "'";
		//procesinam query	
				$result = mysql_query($sql, $connection);
				$tmp = mysql_fetch_array($result);
				$vardas = $tmp['vardas'];
				$pavarde =$tmp['pavarde'];
				$tevavardis = $tmp['tevavardis'];
				$bylosnr = $tmp['bylosnr'];
				$ak = $tmp['ak'];
				$gimimodata = $tmp['gimimodata'];
	} else {
		//$message = "</br> Userid is GET nera. Kursim nauja user'i. </br>";
	};

  //=========================================================================//
 //    					PADARYT NORMALU TIKRINIMA							//
//=========================================================================//
	
	
//patikrint ar visi duomenys ivesti, jeigu ivesti imest i kintamasias ir suprocesinti
	if (!isset($_POST['Submit']) && ($_POST['Submit'])==null ){
		//POST nera
		//$message .= "</br> Forma ne POSTinta";
	} else {
		//POST yra. Tikrinam reiksmes ir imetam kintamasias
		//$message = "We have a POST </br>";
		if (!isset($_POST['vardas']) or ($_POST['vardas'])==null ){
			$message .= " But we don't have vardas! </br>";
		} else {
			$vardas = $_POST['vardas'];
		};
		if (!isset($_POST['pavarde']) or ($_POST['pavarde'])==null ){
			$message .= " But we don't have pavarde! </br>";
		} else {
			$pavarde = $_POST['pavarde'];
		};
		if (!isset($_POST['bylosnr']) or ($_POST['bylosnr'])==null ){
			$message .= " But we don't have Bylos Nr.! </br>";
		} else {
			$bylosnr = $_POST['bylosnr'];
		};
		if (!isset($_POST['gimimodata']) or ($_POST['gimimodata'])==null ){
			$message .= " But we don't have gimimo data! </br>";
		} else {
			$gimimodata = $_POST['gimimodata'];
		};
		if (!isset($_POST['tevavardis']) or ($_POST['tevavardis'])==null ){
			$message .= " But we don't have tevavardis! </br>";
		} else {
			$tevavardis = $_POST['tevavardis'];
		};
		if (!isset($_POST['ak']) or ($_POST['ak'])==null ){
			$message .= " But we don't have ak! </br>";
		} else {
			$ak = $_POST['ak'];
			//echo $ak;
		};
		
		// rasom query naujam vartotojui ir seno updatenimui
		if ($edit == FALSE) {
	// Naujas vartotojas
		$sql = "INSERT INTO `user` (`vardas`, `pavarde`, `tevavardis`, `gimimodata`, `ak`, `bylosnr`) ";
		$sql .= "VALUES (";
		$sql .= "'". $vardas . "', ";
		$sql .= "'". $pavarde . "', ";
		$sql .= "'". $tevavardis . "', ";
		$sql .= "'". $gimimodata . "', ";
		$sql .= "'". $ak . "', ";
		$sql .= "'". $bylosnr . "' ";
		$sql .= ");";
		//echo "SQL naujam: ".$sql; //viskas OK
		
		$result = mysql_query($sql, $connection);
			if ($result) {
				// Success!
				$temp = mysql_insert_id();
				
//---------------------------------------------------------------------------------------------//
//         Pakeisk naujas klientas.php
//----------------------------------------------------------------------------------------------//
				$to = "tautybe.php?new=1&userid=". $temp;
				redirect_to($to);
				//$message .= "</br>Client added Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Subject creation failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		
		} else {
		//Esamo redagavimas
		$sql = "UPDATE `user` SET";
		$sql .= " `vardas` =";
		$sql .= "'". $vardas . "', ";
		$sql .= " `pavarde` =";
		$sql .= "'". $pavarde . "', ";
		$sql .= " `tevavardis` =";
		$sql .= "'". $tevavardis . "', ";
		$sql .= " `gimimodata` =";
		$sql .= "'". $gimimodata . "', ";
		$sql .= " `ak` =";
		$sql .= "'". $ak . "', ";
		$sql .= " `bylosnr` =";
		$sql .= "'". $bylosnr . "' ";
		$sql .= " WHERE `userid` = ";
		$sql .= ($_GET['userid']);
		$sql .= " LIMIT 1;";
		
		$result = mysql_query($sql, $connection);
			if ($result) {
				$to = "summary.php?userid=" . $_GET['userid'];
				redirect_to($to);
				// Success!
				//$message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		
		// echo "SQL esamam: ".$sql; //HOORAY, veikia!!!!
		};
	};

	


	
	
?>

			
					<article>
						<h2><?php 
						if ($edit) { echo "Esamo kliento redagavimas";} 
						else { echo "Naujo kliento ivedimas";};
						?></h2>
						<p>Pagrindiniai duomenys</p>
						<br>
						<?php echo $message;
						?>
					  <form autocomplete="off" action="naujas_klientas.php<?php 
					  if ($edit) {echo "?userid=" . $_GET['userid'];};
					  ?>" method="post" name="search" accept-charset="utf-8">
					    <p>
					    </p>
					    <table   border="1">
					      <tr>
					        <td>Vardas</td>
					        <td width="150"><input name="vardas" type="text" value="<?php
							echo $vardas;
							?>" required/></td>
					        <td>Pavarde</td>
					        <td width="150"><input name="pavarde" type="text" value="<?php
							echo $pavarde;
							?>" required /></td>
							<td>Tevavardis</td>
					        <td width="150"><input name="tevavardis" type="text" value="<?php
							echo $tevavardis;
							?>"  /></td>
				          </tr>
						  <tr>
							<td> Bylos Nr.</td>
					        <td width="150"><input name="bylosnr" type="text" value="<?php
							echo $bylosnr;
							?>" required/></td>
							<td> Asm. kodas</td>
					        <td width="150"><input name="ak" type="text" value="<?php
							echo $ak;
							?>" /></td>
							<td> Gimimo data</td>
					        <td width="150"><input name="gimimodata" type="date" value="<?php
							echo $gimimodata;
							?>" placeholder="1900-12-31" required/></td>	
						  </tr>
				        </table>
					    <br>
					    <p>
					      <input type="reset" name="Reset" id="button" value="Valyti laukus"><input name="Submit" type="submit" value="<?php if ($edit) {echo "Redaguoti";} else {echo "Pridėti klientą";};?>" /> 
					    </p>
					  </form>
 
			</article>
            
<?php require("includes/footer.php"); ?>